package com.itboye.hutouben.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.base.MyApplcation;
import com.itboye.hutouben.bean.GoodsModel;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.SPUtils;
import com.itboye.hutouben.util.XImageLoader;
import com.itboye.hutouben.widget.LoweImageView;

import java.util.List;

/**
 * 商品列表
 */
public class GoodsListAdapter extends BaseAdapter {
    List<GoodsModel.GoodsList> goodsList;
    Activity activity;

    public GoodsListAdapter(List<GoodsModel.GoodsList> goodsList, Activity activity) {
        this.goodsList = goodsList;
        this.activity = activity;
    }

    @Override
    public int getCount() {
        return goodsList.size();
    }

    @Override
    public Object getItem(int position) {
        return goodsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (holder == null) {
            holder=new ViewHolder();
            convertView = LayoutInflater.from(activity).inflate(R.layout.item_goodslist, null);
            holder.goods_img= (LoweImageView) convertView.findViewById(R.id.goods_img);
            holder.good_title= (TextView) convertView.findViewById(R.id.good_title);
            holder.good_jieshao= (TextView) convertView.findViewById(R.id.good_jieshao);
            holder.good_price= (TextView) convertView.findViewById(R.id.good_price);
            holder.goods_tehui= (ImageView) convertView.findViewById(R.id.goods_tehui);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        XImageLoader.load(Const.IMAGE_HEAD + goodsList.get(position).getMain_img(),
                holder.goods_img);
        holder.good_title.setText(goodsList.get(position).getName());
        holder.good_jieshao.setText(goodsList.get(position).getSecondary_headlines());
        if (goodsList.get(position).getPrice().equals("")) {

        }else {
            holder.good_price.setText("¥" + Double.parseDouble(goodsList.get(position).getPrice()) / 100);
        }
        boolean isTeHui = false;
        if(goodsList.get(position).get_group()!=null){
            if(goodsList.get(position).get_group().size()>0){
                for (int i=0;i<goodsList.get(position).get_group().size();i++){
                    if(goodsList.get(position).get_group().get(i).getG_id().equals("6204")){
                        isTeHui = true;
                    }else{
//                        holder.goods_tehui.setVisibility(View.GONE);
                    }
                }
            }
        }
        setTeHui(isTeHui,holder);

        return convertView;
    }


    private void setTeHui(boolean isTeHui, ViewHolder holder) {
        holder.goods_tehui.setVisibility(isTeHui?View.VISIBLE:View.GONE);
        if (isTeHui) {
            String uid = (String) SPUtils.get(MyApplcation.ctx, null, Const.JiaoBiao, "");
            XImageLoader.load(Const.IMAGE_HEAD + uid,
                    holder.goods_tehui);
        }

    }

    public static class ViewHolder {
        LoweImageView goods_img;
        ImageView goods_tehui;
        TextView good_title, good_jieshao, good_price;
    }
}
